package com.yuncool.sky.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yuncool.sky.system.domain.CourseClassify;
import com.yuncool.sky.system.mapper.CourseClassifyMapper;
import com.yuncool.sky.system.pojo.bo.CourseBO;
import com.yuncool.sky.system.pojo.form.CourseForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther XIE
 * @Date 2019/11/22 22:17
 * @Description 课程分类服务类
 * @Version 1.0
 */
@Service
public class CourseClassifyService extends ServiceImpl<CourseClassifyMapper, CourseClassify> {

    @Autowired
    CourseClassifyMapper courseClassifyMapper;

    /**
     * 课程分类列表
     * 有缓存数据
     *
     * @return
     */
    @Cacheable(value = "studyList")
    public List<CourseBO> studyList() {
        List<CourseBO> courseBOList = courseClassifyMapper.selectList(new QueryWrapper<>())
                .stream()
                .sorted(Comparator.comparing(CourseClassify::getId))
                .map(courseClassify -> new CourseBO(courseClassify.getTitle(), courseClassify.getImgUrl(), courseClassify.getId().toString()))
                .collect(Collectors.toList());
        return courseBOList;
    }

    /**
     * 后台:课程分类列表
     *
     * @param courseForm
     * @return
     */
    public List<CourseClassify> sysCourseClassifyList(CourseForm courseForm) {
        PageHelper.startPage(courseForm.getPages(), courseForm.getPageSize());
        return this.list();
    }
}
